var Workport = Workport || {};
//@Path("/IWantTo")
Workport.WantAPI = (function(){
	var root = WP_RESTFUL_URL + (WP_RESTFUL_URL.endsWith('/') ? '' : '/') + 'IWantTo/';
	
	var urls = ["HomeAppsList", "AppsList", "SearchHomeApps", "SearchApps","{1}/AddFavourite", "{1}/Tabs" ];
	
	function getUrl(/*index, replace1, replace2, ...*/) {
		var index, i, loops, r;
		loops = arguments.length;
		if (loops > 0) {
			index = arguments[0];
			r = root + urls[index];
			for (i = 1; i < loops; i++)
				r = r.replace("{"+i+"}", arguments[i]);
		}
		return r;
	}
	
	return {
		// @GET  @Path("/HomeAppsList")
		// public HomeAppResponse GetHomeApps()
		homeApps:function(handler,request){
			return sfw.Http.get(getUrl(0),function(xhr, data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				Offset:request.offset,
				Count:request.count,
				FilterBy:request.filterBy,
				orderBy:request.orderBy,
				Mode:request.mode
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET	@Path("/AppsList")
		//public AppResponse GetApps()
		allApps:function(handler,request){
			return sfw.Http.get(getUrl(1),function(xhr, data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				Offset:request.offset,
				Count:request.count,
				FilterBy:request.filterBy,
				orderBy:request.orderBy,
				Mode:request.mode
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET	@Path("/SearchHomeApps")
		// public AppResponse GetHomeAppsSearchResult()
		searchHomeApp:function(handler,request){
			return sfw.Http.get(getUrl(2),function(xhr, data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				Offset:request.offset,
				Count:request.count,
				SearchText:request.key,
				Filtermode:request.mode
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET	@Path("/SearchApps")
		// public AppResponse GetAppsSearchResult()
		searchApp:function(handler,request){
			return sfw.Http.get(getUrl(3),function(xhr, data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			},{
				Offset:request.offset,
				Count:request.count,
				SearchText:request.key,
				Filtermode:request.mode
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET	@Path("/{AppId}/AddFavourite")
		// public AddFavouriteResponse GetAddFavourite(@PathParam("AppId") int appID)
		addFavourite:function(handler,request){
			var url = "IWantTo/"+request.appId+"/AddFavourite";
			return sfw.Http.get(getUrl(4,request.appId),function(xhr, data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET	@Path("/{AppId}/Tabs")
		// public TabResponse GetTabs(@PathParam("AppId") int appID)
		getTabs:function(handler,request){
			var url = "IWantTo/"+request.appId+"/Tabs";
			return sfw.Http.get(getUrl(5,request.appId),function(xhr, data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(toViewModel(data));
					}
				}
			});
			function toViewModel(data){
				return data;
			}
		}
	}
})();
